//
//  JMProfessionalSettingViewController.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/9/6.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMProfessionalSettingViewController.h"
#import "JMProfessionalSettingViewMode.h"
#import "JMProfessionalSettingInfoViewController.h"
#import "JMConcernProfessional.h"
#import "JMBasieTableViewCell.h"
#import "JMLoginViewController.h"


#import "JMAcademSearchViewController.h"



@interface JMProfessionalSettingViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
    int lastId;
}

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UIButton *searchBtn;

@end

@implementation JMProfessionalSettingViewController

- (UITableView *)tableView
{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.backgroundView.backgroundColor = [UIColor clearColor];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.frame = self.view.bounds;
        _tableView = tableView;
    }
    return _tableView;
}

- (UIButton *)searchBtn
{
    if (!_searchBtn) {
        UIButton *button = [[UIButton alloc]init];
        [button setTitleColor:UIColorFromRGB(0xeb6100) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(searchBtnButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [button setImage:[UIImage imageNamed:@"projectImage.bundle/search_image"] forState:UIControlStateNormal];
        button.frame = CGRectMake(UI_SCREEN_WITH - 44, 0, 44, 44);
        [self.view addSubview:_searchBtn = button];
    }
    return _searchBtn;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _dataArray;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = NO;
    self.backBtn.hidden =YES;
    self.navigationItem.title = @"专业设置";
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:self.searchBtn];
    self.navigationItem.rightBarButtonItem =item;
}

-(void)searchBtnButtonClick:(UIButton *)search{
    JMAcademSearchViewController *vc = [JMAcademSearchViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];    
    lastId = 0;
    [self setupRefresh];
}
#pragma mark 上下拉刷新数据
- (void)setupRefresh{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    [self.tableView.header beginRefreshing];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
}
-(void)headerRereshing{
    lastId = 0;
    [self getMessageFromeServer:nil];
}
-(void)footerRereshing{
    [self getMessageFromeServer:nil];
}

#pragma mark  tableView delegate dasouce

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return  self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString * str=@"ID";
        JMBasieTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
        if(!cell){
            cell = [[JMBasieTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        JMConcernProfessional *mode = self.dataArray[indexPath.row];
        cell.textLabel.text = mode.titleName;
        cell.textLabel.font = [UIFont systemFontOfSize:UI_SCREEN_WITH *0.0375];
     return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    JMProfessionalSettingInfoViewController *vc = [JMProfessionalSettingInfoViewController new];
    vc.mode = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UI_SCREEN_WITH *0.09375;
}

#pragma mark xia zai shu ju
-(void)getMessageFromeServer:(NSDictionary *)newDict{
    [JMTools showAlertView:self.view];
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if(cf.user){
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:cf.user.userId forKey:@"user"];
        [dict setObject:cf.user.token forKey:@"token"];
        [dict setObject:[NSString stringWithFormat:@"%d",lastId] forKey:@"last"];
        [dict setObject:@"20"forKey:@"size"];
        
        [JMProfessionalSettingViewMode fetchProfessionalSettingListParametes:dict block:^(id data, NSError *error, id currData) {
            [JMTools hiddenAlerView:self.view];

            if([[data objectForKey:@"status"] integerValue] == 1){
                NSArray *array = (NSArray *)currData;
                if(lastId == [[currData lastObject] professionalId]){
                    [JMTools showAlertView:self.view message:@"没有更多数据"];
                    return ;
                }else{
                    [self.dataArray addObjectsFromArray:array];
                    lastId = [[currData lastObject] professionalId];
                    [self.tableView reloadData];
                    [JMTools showAlertView:self.view message:data[@"message"]];
                }
            }else{
                [JMTools showAlertView:self.view message:data[@"message"]];
                JMLoginViewController *vc =[JMLoginViewController new];
                JMNavigationController *nav = [[JMNavigationController alloc]initWithRootViewController:vc];
                [self presentViewController:nav animated:YES completion:^{
                    
                }];
            }
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
